package com.ruoyi.business.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.BusZhncPlant;
import com.ruoyi.business.service.IBusZhncPlantService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 植物工厂产值Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/zhnc/plant")
@Api(tags = "智慧农场-植物工厂产值")
public class BusZhncPlantController extends BaseController {
    @Autowired
    private IBusZhncPlantService busZhncPlantService;


    /**
     * 查询植物工厂产值列表
     */
    @GetMapping("/data")
    @ApiOperation("图表数据")
    public AjaxResult data(BusZhncPlant busZhncPlant) {
        startPage();
        List<BusZhncPlant> list = busZhncPlantService.selectBusZhncPlantList(busZhncPlant);
        Map<String, List> map = new HashMap<>();
        List<String> keyList = new ArrayList<>();
        List<Object> leafList = new ArrayList<>();
        List<Object> fruitList = new ArrayList<>();
        list.forEach(o -> {
            Date time = o.getTime();
            String t = DateUtils.parseDateToStr(DateUtils.YYYYMM_NIAN, time);
            keyList.add(t);
            leafList.add(o.getLeaf());
            fruitList.add(o.getFruit());
        });

        Collections.reverse(keyList);
        Collections.reverse(leafList);
        Collections.reverse(fruitList);
        map.put("keyList", keyList);
        map.put("leafList", leafList);
        map.put("fruitList", fruitList);
        return AjaxResult.success(map);
    }


    /**
     * 查询植物工厂产值列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BusZhncPlant busZhncPlant) {
        startPage();
        List<BusZhncPlant> list = busZhncPlantService.selectBusZhncPlantList(busZhncPlant);
        return getDataTable(list);
    }

    /**
     * 导出植物工厂产值列表
     */
    @PreAuthorize("@ss.hasPermi('zhnc:plant:export')")
    @Log(title = "植物工厂产值", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusZhncPlant busZhncPlant) {
        List<BusZhncPlant> list = busZhncPlantService.selectBusZhncPlantList(busZhncPlant);
        ExcelUtil<BusZhncPlant> util = new ExcelUtil<BusZhncPlant>(BusZhncPlant.class);
        util.exportExcel(response, list, "植物工厂产值数据");
    }

    /**
     * 获取植物工厂产值详细信息
     */
    @PreAuthorize("@ss.hasPermi('zhnc:plant:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(busZhncPlantService.selectBusZhncPlantById(id));
    }

    /**
     * 新增植物工厂产值
     */
    @PreAuthorize("@ss.hasPermi('zhnc:plant:add')")
    @Log(title = "植物工厂产值", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusZhncPlant busZhncPlant) {
        return toAjax(busZhncPlantService.insertBusZhncPlant(busZhncPlant));
    }

    /**
     * 修改植物工厂产值
     */
    @PreAuthorize("@ss.hasPermi('zhnc:plant:edit')")
    @Log(title = "植物工厂产值", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusZhncPlant busZhncPlant) {
        return toAjax(busZhncPlantService.updateBusZhncPlant(busZhncPlant));
    }

    /**
     * 删除植物工厂产值
     */
    @PreAuthorize("@ss.hasPermi('zhnc:plant:remove')")
    @Log(title = "植物工厂产值", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(busZhncPlantService.deleteBusZhncPlantByIds(ids));
    }
}
